<?php

class Admin_PermissionsController extends Zend_Controller_Action
{

    function init()
    {
        $this->params = $this->getRequest()->getParams();
        $response = $this->getResponse();
        $response->insert('header', $this->view->render('/blocks/header.phtml'));
        $response->insert('left', $this->view->render('/blocks/left.phtml'));
    }

    public function indexAction()
    {
        $statusInfo = $this->checkControllersAndActions();
        $this->view->delete = $statusInfo['delete'];
        $this->view->add = $statusInfo['add'];

        $ACL = new Core_ACL();
        $this->view->ALCCount = $ACL->getACLCount();
        $this->view->ControllerCount = $ACL->getControllersCount();
        $this->view->controllers = $ACL->getControllers();
        $this->view->ACL = $ACL->getAllACL();

        $roles = new Core_Role();
        $this->view->roles = $roles->getAll();
    }

    public function advanceAction()
    {
        $statusInfo = $this->checkControllersAndActions();
        $this->view->delete = $statusInfo['delete'];
        $this->view->add = $statusInfo['add'];

        $ACL = new Core_ACL();
        $this->view->controllerAction = $ACL->getControllerAction();
        $this->view->ACL = $ACL->getACL();

        $roles = new Core_Role();
        $this->view->roles = $roles->getAll();
    }

    /*
     * Check controllers and actions
     */
    private function checkControllersAndActions()
    {
        /*
         * Get all controllers and actions from the proyect
         */
        $front = $this->getFrontController();
        $controllerActionsArray = array();
        foreach ($front->getControllerDirectory() as $module => $path) {
            foreach (scandir($path) as $file) {
                if (strstr($file, "Controller.php") !== false) {
                    include_once $path . DIRECTORY_SEPARATOR . $file;
                    foreach (get_declared_classes () as $class) {
                        if (!$acl[strtolower(substr($class, 0, strpos($class, "Controller")))]) {
                            if (is_subclass_of($class, 'Zend_Controller_Action')) {
                                $controller = strtolower(substr($class, 0, strpos($class, "Controller")));
                                $actions = array();
                                foreach (get_class_methods($class) as $action) {
                                    if (strstr($action, "Action") !== false) {
                                        $actions[] = $action;
                                    }
                                }
                            }
                        }
                    }
                    $controllerActionsArray[$controller] = $actions;
                }
            }
        }

        $ACL = new Core_ACL();
        $allControllers = array();
        //Check if controller and action exists into DB
        foreach ($controllerActionsArray as $controller => $actions) {
            $allAction = array();
            //Check if controller exists into DB
            $checkController = $ACL->getControllerByName($controller);
            if (!$checkController) {
                //if not exists the controller add into DB and get the id.
                $idController = $ACL->insertController($controller);
                $itemsAdded['controllers'][] = $controller;
            } else {
                //if exists the controller get the idController
                $idController = $checkController[0]['id'];
            }
            $allControllers[] = $controller;
            foreach ($actions as $action) {
                list($action, $a) = explode('Action', $action);
                // check if the action exists into DB
                $checkAction = $ACL->getAccionByAcionIDControlador($action, $idController);
                if (!$checkAction) {
                    //if not exists the action add into DB and get the id.
                    $idAction = $ACL->insertAction($action, $idController);
                    $itemsAdded['actions'][$controller][] = $action;
                } else {
                    //if exists the action get the idAction
                    $idAction = $checkAction[0]['id'];
                }

                $allAction[] = $action;
            }
            //Get all actions unused for this controller
            $unusedActions = $ACL->getActionUnused($allAction, $idController);
            if ($unusedActions) {
                foreach ($unusedActions as $toDelete) {
                    //delete Action
                    $ACL->deleteActionById($toDelete['id']);
                    //delete ACL for this action
                    $ACL->deleteACLByIDAction($toDelete['id']);
                    $itemsDeleted['actions'][$controller][] = $toDelete['action'];
                }
            }
        }
        //Get all controllers unused
        $unusedController = $ACL->getControllersUnused($allControllers);
        if ($unusedController) {
            foreach ($unusedController as $toDelete) {
                $itemsDeleted['controllers'][] = $toDelete['controllers'];
                //get all action for this controller
                $actions = $ACL->getActionByIDController($toDelete['id']);
                $ACL->deleteActionByIDController($toDelete['id']);
                if ($actions) {
                    foreach ($actions as $action) {
                        $itemsDeleted['actions'][$toDelete['controllers']][] = $action['action'];
                        $ACL->deleteACLByIDAction($action['id']);
                    }
                }
                $ACL->deleteControllerByID($toDelete['id']);
            }
        }
        $dataArray = array();
        $dataArray['delete'] = $itemsDeleted;
        $dataArray['add'] = $itemsAdded;
        return $dataArray;
    }

    #Funciones que se encargan de guardar los permisos
    public function saveaclcontrollerAction()
    {
        $this->_helper->layout->disableLayout();
        $param = $this->_getAllParams();
        $ACL = new Core_ACL();
        list($controller, $userRole) = explode("-", $param['data']);
        $controllerAction = $ACL->getControllerActionByIDController($controller);
        foreach ($controllerAction as $controller) {
            $checkACL = $ACL->getACLByActionRole($controller['id'], $userRole);
            if ($checkACL) {
                $ACL->updateACL($checkACL[0]['id'], $controller['id'], $userRole, $param['option']);
            } else {
                $ACL->insertACL($controller['id'], $userRole, $param['option']);
            }
        }
    }

    public function saveaclAction()
    {
        $this->_helper->layout->disableLayout();
        $param = $this->_getAllParams();
        $ACL = new Core_ACL();
        list($controller, $action, $userRole) = explode("-", $param['data']);
        $checkController = $ACL->getControllerByName($controller);
        $idController = $checkController[0]['id'];
        $checkAction = $ACL->getAccionByAcionIDControlador($action, $idController);
        $checkACL = $ACL->getACLByActionRole($checkAction[0]['id'], $userRole);
        if ($checkACL) {
            $ACL->updateACL($checkACL[0]['id'], $checkAction[0]['id'], $userRole, $param['option']);
        } else {
            $ACL->insertACL($checkAction[0]['id'], $userRole, $param['option']);
        }
    }

}